const BASE_URL = 'http://api-breakingnews-web.itheima.net'
let total=0
let query={
  pagenum:1,
  pagesize:4,
  cate_id:"",
  state:"",
}
function loadList(){
  axios
  .get(`${BASE_URL}/my/article/list`,{
    headers:{
      Authorization: localStorage.getItem("token"),
    },
    params:query,
  })
  .then(res=>{
    console.log("获取文章列表",res.data)
    total=res.data.total
    setPage()
    $("tbody").empty()
    res.data.data.forEach(v=>{
      let theTr = `<tr>
                    <td>${v.title}</td>
                    <td>${v.cate_name}</td>
                    <td>${moment(v.pub_date).format('YYYY-MM-DD hh:mm:ss a')}</td>
                    <td>${v.state}</td>
                    <th>
                    <button myid="${
                      v.Id
                    }" type="button" class="layui-btn layui-btn-xs layui-btn-danger del">删除</button>
                    </th>
                </tr>`
        $('tbody').append(theTr)
    })
  })
}
loadList()
function loadCates(){
  axios
  .get(`${BASE_URL}/my/article/cates`,{
    headers:{
      Authorization: localStorage.getItem('token'),
    },
  })
  .then(res=>{
    console.log("类别",res.data)
    res.data.data.forEach(v=>{
      var theOption = `<option value="${v.Id}" lay-verify="cate">${v.name}</option>`
      $('select[name=category]').append(theOption)
    })
    let form=window.layui.form
    form.render("select",'category')
  })
  
}
loadCates()
$(".search").on("submit",function(e){
  e.preeventDefault()
  query.cate_id=$("select[name=category]").val()
  query.state=$("select[name=state]").val()
  loadList()
})
function setPage(){
  var laypage=layui.laypage
  laypage.render({
    elem: 'page', // 布局分页的容器 ID
    count: total, //数据总数，从服务端得到
    // 设置 每页的个数
    limit: query.pagesize,
    // 设置 页码
    curr: query.pagenum,
    // 跳
    jump: function (obj, first) {
      if (!first) {
        //2. 修改参数
        query.pagenum = obj.curr
        query.pagesize = obj.limit
        //3. 发送请求
        loadList()
      }
    },
  })
}
setPage()
$("tbody").on("click",".del",function(){
  let id=$(this).attr("myid")
  console.log(id)
  axios.get(`${BASE_URL}/my/article/delete/${id}`,{
    headers:{
      Authorization:localStorage.getItem("token"),
    },
  })
  .then(res=>{
    console.log(res.data)
    if(res.data.status===0){
      layer.msg(res.data.message,{icon:1})
      loadList()
    }
  })
})
